package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.bo.FullReductionBO;
import com.junzhiit.nengji.bo.FullReductionConditionBO;
import com.junzhiit.nengji.bo.FullReductionListBO;
import com.junzhiit.nengji.cms.service.FullReductionService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.FullReductionConditionVO;
import com.junzhiit.nengji.cms.web.vo.FullReductionGoodsVO;
import com.junzhiit.nengji.cms.web.vo.FullReductionListVO;
import com.junzhiit.nengji.cms.web.vo.FullReductionVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.FullReductionConditionDAO;
import com.junzhiit.nengji.dao.FullReductionDAO;
import com.junzhiit.nengji.dao.FullReductionGoodsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.FullReduction;
import com.junzhiit.nengji.model.FullReductionCondition;
import com.junzhiit.nengji.model.FullReductionGoods;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.enums.FullReductionStatus;

import cn.hutool.core.bean.BeanUtil;

@Service
public class FullReductionServiceImpl implements FullReductionService {

	@Autowired
	FullReductionDAO fullReductionDAO;

	@Autowired
	SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	ShopDAO shopDAO;

	@Autowired
	FullReductionConditionDAO fullReductionConditionDAO;

	@Autowired
	FullReductionGoodsDAO fullReductionGoodsDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, FullReductionListVO frlv) {
		SystemUserSessionVO sessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(frlv.getSessionId()),
				SystemUserSessionVO.class);
		String systemId = sessionVO.getSystemId();
		Shop shop = shopDAO.query(systemId);
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		FullReductionListBO frlb = new FullReductionListBO();
		BeanUtil.copyProperties(frlv, frlb);
		frlb.setCsId(systemId);
		if (frlb.getStartTime() != null) {
			frlb.setStartTime(DateUtil.getDateStart(frlb.getStartTime()));
		}
		if (frlb.getEndTime() != null) {
			frlb.setEndTime(DateUtil.getDateEnd(frlb.getEndTime()));
		}
		List<FullReductionListBO> frbList = fullReductionDAO.findAll(frlb, dtp.getStart(), dtp.getLength());
		Integer totalCount = fullReductionDAO.getTotalCount(frlb);
		return ViewObjectUtil.dataTables(frbList, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO addAndEditFullReduction(String fullReductionVO, String fullReductionConditionVO,
			String fullReductionGoodsVO, String sessionId) {
		ResultVO result = new ResultVO();
		FullReductionVO frVO = JsonUtil.toObject(fullReductionVO, FullReductionVO.class);
		List<FullReductionConditionVO> frcVOList = JsonUtil.toListObject(fullReductionConditionVO,
				FullReductionConditionVO.class);
		FullReductionGoodsVO frgVO = JsonUtil.toObject(fullReductionGoodsVO, FullReductionGoodsVO.class);

		SystemUserSessionVO sessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		String systemId = sessionVO.getSystemId();
		Shop shop = shopDAO.query(systemId);
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		FullReduction fr = new FullReduction();
		BeanUtil.copyProperties(frVO, fr);
		Date now=new Date();
		fr.setStartTime(DateUtil.getDateStart((DateUtil.parseDate(frVO.getStartTime()))));
		fr.setEndTime((DateUtil.getDateEnd(DateUtil.parseDate(frVO.getEndTime()))));
		if(fr.getStartTime().getTime()>now.getTime()){
			fr.setStatus(FullReductionStatus.NOTSTARTED.toIntValue());
		}
		if(fr.getEndTime().getTime()<now.getTime()){
			fr.setStatus(FullReductionStatus.FINISHED.toIntValue());
		}
		if(fr.getStartTime().getTime()<now.getTime()&&fr.getEndTime().getTime()>now.getTime()){
			fr.setStatus(FullReductionStatus.STARTED.toIntValue());	
		}
		FullReduction frReturn = new FullReduction();
		if (StringUtils.isNoneBlank(frVO.getId())) {
			fullReductionDAO.update(fr);
			frReturn.setId(frVO.getId());
		} else {
			fr.setCsId(systemId);
			frReturn = fullReductionDAO.insert(fr);
		}

		fullReductionConditionDAO.deleteByFullReductionId(frReturn.getId());
		List<FullReductionCondition> frcList = new ArrayList<FullReductionCondition>();
		for (FullReductionConditionVO frcVO : frcVOList) {
			FullReductionCondition frc = new FullReductionCondition();
			BeanUtil.copyProperties(frcVO, frc);
			frc.setFullReductionId(frReturn.getId());
			frcList.add(frc);
		}
		fullReductionConditionDAO.batchInsert(frcList);

		fullReductionGoodsDAO.deleteByFullReductionId(frReturn.getId());
		String[] goodsIdArr = frgVO.getGoodsIdArr();
		if (goodsIdArr.length > 0) {
			List<FullReductionGoods> frgList = new ArrayList<FullReductionGoods>();
			for (String goodsId : goodsIdArr) {
				FullReductionGoods frg = new FullReductionGoods();
				frg.setGoodsId(goodsId);
				frg.setFullReductionId(frReturn.getId());
				frgList.add(frg);
			}
			fullReductionGoodsDAO.batchInsert(frgList);
		}
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		fullReductionDAO.delete(id);
		fullReductionConditionDAO.deleteByFullReductionId(id);
		fullReductionGoodsDAO.deleteByFullReductionId(id);
		return result;
	}

	@Override
	public ResultVO findById(FullReductionVO frv) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO sessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(frv.getSessionId()),
				SystemUserSessionVO.class);
		String systemId = sessionVO.getSystemId();
		Shop shop = shopDAO.query(systemId);
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		Map<String, Object> map = new HashMap<>();
		FullReduction fr = fullReductionDAO.query(frv.getId());
		FullReductionBO frb = new FullReductionBO();
		BeanUtil.copyProperties(fr, frb);
		map.put("fullReductionVO", JsonUtil.toJson(frb));
		List<FullReductionConditionBO> frcbList = fullReductionConditionDAO.findByFullReductionId(frv.getId());
		map.put("fullReductionConditionVO", JsonUtil.toJson(frcbList));
		List<FullReductionGoods> frgList = fullReductionGoodsDAO.findByFullReductionId(frv.getId());
		FullReductionGoodsVO frgVO = new FullReductionGoodsVO();
		if (!frgList.isEmpty()) {
			String[] goodsIdArr = new String[frgList.size()];
			for (int i = 0; i < frgList.size(); i++) {
				goodsIdArr[i] = frgList.get(i).getGoodsId();
			}
			frgVO.setGoodsIdArr(goodsIdArr);
		}
		map.put("fullReductionGoodsVO", JsonUtil.toJson(frgVO));
		result.setData(map);
		return result;
	}

	@Override
	public ResultVO updateFullReductionStatus() {
		ResultVO result = new ResultVO();
		List<FullReduction> fullReductionList = fullReductionDAO.getAll();
		Date now = new Date();
		for (FullReduction fullReduction : fullReductionList) {
			if (now.getTime() >= fullReduction.getStartTime().getTime()
					&& now.getTime() <= fullReduction.getEndTime().getTime()) {
				fullReduction.setStatus(FullReductionStatus.STARTED.toIntValue());
			}
			if (now.getTime() > fullReduction.getEndTime().getTime()) {
				fullReduction.setStatus(FullReductionStatus.FINISHED.toIntValue());
			}
		}
		fullReductionDAO.batchUpdate(fullReductionList);
		return result;
	}
}
